﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DialogForm
{
    public partial class FrmDepartment_dlg : DevExpress.XtraEditors.XtraForm
    {
        public delegate void ReturnListEventHandler(List<DataRow> lists);
        public event ReturnListEventHandler ReturnList;

        public FrmDepartment_dlg()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmDepartment_dlg_Load(object sender, EventArgs e)
        {
            LoadData();

            tlsMain.NodesIterator.DoOperation(new CommonControl.DxGridHelper.CollapseExceptSpecifiedOperation(tlsMain.FocusedNode));
        }


        #region ================================功能函数区=====================================
        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            var bll = new DMZS.BLL.DepartmentNew();
            var ds = bll.GetListForDialog(txtCode.Text.Trim(), txtName.Text.Trim());
            tlsMain.DataSource = ds.Tables[0];

            tlsMain.ExpandAll();
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            CommonControl.DxGridHelper.InitGridStyle(tlsMain);
        }
        #endregion ================================功能函数区=====================================

        private void btnSearch_Click(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (tlsMain.FocusedNode != null)
            {
                List<DataRow> list = new List<DataRow>();
                var view = tlsMain.GetDataRecordByNode(tlsMain.FocusedNode) as DataRowView;
                list.Add(view.Row);
                ReturnList(list);
                this.Close();
            }
        }

        private void tlsMain_DoubleClick(object sender, EventArgs e)
        {
            if (tlsMain.FocusedNode != null)
            {
                List<DataRow> list = new List<DataRow>();
                var view = tlsMain.GetDataRecordByNode(tlsMain.FocusedNode) as DataRowView;
                list.Add(view.Row);
                ReturnList(list);
                this.Close();
            }
        }

        private void tlsMain_CustomDrawNodeIndicator(object sender, DevExpress.XtraTreeList.CustomDrawNodeIndicatorEventArgs e)
        {
            if (tlsMain.FocusedNode.HasChildren) return;

            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }
    }
}